package com.fosss.hashTable;

import java.util.Scanner;

public class HashTableDemo {
    public static void main(String[] args) {
        HashTable hashTable = new HashTable(7);

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请选择要进行的操作：");
            System.out.println("add:添加操作");
            System.out.println("list:遍历操作");
            System.out.println("find:根据id查找");
            System.out.println("delete:删除操作");
            System.out.println("exit:退出程序");

            String input = sc.next();
            switch (input) {
                case "add":
                    System.out.println("请输入编号：");
                    int id = sc.nextInt();
                    System.out.println("请输入姓名：");
                    String name = sc.next();
                    Employee employee = new Employee(id, name);
                    hashTable.add(employee);
                    break;
                case "list":
                    hashTable.list();
                    break;
                case "find":
                    System.out.println("请输入要查找的id:");
                    int id2 = sc.nextInt();
                    hashTable.findById(id2);
                    break;
                case "delete":
                    System.out.println("请输入id:");
                    int id3 = sc.nextInt();
                    hashTable.deleteById(id3);
                    break;
                case "exit":
                    sc.close();
                    System.exit(0);
            }
        }
    }
}

//表示一个雇员
class Employee {
    public int id;
    public String name;
    public Employee next;

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' + '}';
    }
}

//创建链表
class HashLinkedList {
    private Employee head;

    //添加雇员
    public void add(Employee employee) {
        if (head == null) {
            head = employee;//直接指向数据，没有设置头节点
            return;//!!!!!!添加完后要结束，防止进行下边的代码
        }
        //定义一个辅助指针
        Employee current = head;
        while (current.next != null) {//找到最后一个节点
            current = current.next;
        }
        //默认将新雇员添加到最后
        current.next = employee;
    }

    //遍历雇员
    public void list(int id) {
        if (head == null) {
            System.out.println("第" + (id + 1) + "条链表为空");
            return;
        }
        Employee current = head;
        System.out.printf("第%d条链表", (id + 1));
        while (true) {
            System.out.printf("=> id=%d name=>%s\t", current.id, current.name);
            if (current.next == null) {
                break;
            }
            current = current.next;
        }
        System.out.println();
    }

    //根据id查找雇员
    public Employee findById(int id) {
        if (head == null) {
            System.out.println("没有找到");
            return null;
        }
        Employee current = head;
        boolean flag = false;//判定是否找到
        while (true) {
            if (current.id == id) {
                flag = true;
                break;
            }
            if (current.next == null) {
                break;
            }
            current = current.next;
        }
        if (flag) {
            //找到了
            return current;
        }
        return null;
    }

    //根据id删除雇员
    public void deleteById(int id) {
        if (head == null) {
            System.out.println("链表为空");
            return;
        }
        //只有一个节点时
        if (head.id == id) {
            head = head.next;
            System.out.println("删除成功了泥");
            return;
        }
        Employee current = head;
        boolean flag = false;
        while (true) {
            if (current.next == null) {
                break;
            }
            if (current.next.id == id) {
                flag = true;
                break;
            }

            current = current.next;
        }

        if (flag) {
            //找到了，进行删除--要删除的是找到的下一个结点
            current.next=current.next.next;
            System.out.println("删除成功了泥");
        }
    }
}

//创建哈希表，管理链表数组
class HashTable {
    private HashLinkedList[] hashListedListArray;
    private int size;

    //提供构造器，初始化数组
    public HashTable(int size) {
        this.size = size;
        hashListedListArray = new HashLinkedList[size];
        //注意这里有一个坑！！！！！！！！必须初始化每条链表，否则会出现空指针异常
        for (int i = 0; i < size; i++) {
            hashListedListArray[i] = new HashLinkedList();
        }
    }

    //散列函数
    public int hashFun(int id) {
        return id % size;
    }

    //添加方法
    public void add(Employee employee) {
        int index = hashFun(employee.id);
        hashListedListArray[index].add(employee);
    }

    //遍历方法
    public void list() {
        for (int i = 0; i < size; i++) {
            hashListedListArray[i].list(i);
        }
    }

    //根据id查找
    public void findById(int id) {
        Employee employee = null;

        //使用散列函数确定到哪条链表查找
        int index = hashFun(id);
        employee = hashListedListArray[index].findById(id);
        if (employee != null) {
            System.out.println(employee);
        } else {
            System.out.println("无此人");
        }
    }
    //删除操作
    public void deleteById(int id){
        //使用散列函数确定到哪条链表查找
        int index = hashFun(id);
        hashListedListArray[index].deleteById(id);
    }
}









